package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategroyService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategroyService categroyService;

    /**
     * 商品展示
     * @return
     */
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order
                       ){
        IPage<DtsCategory> iPage = categroyService.queryCategroy(page, limit, sort, order);
        long total = iPage.getTotal();
        List<DtsCategory> records = iPage.getRecords();
        Map<String,Object> data=new HashMap<>();
        data.put("total",total);
        data.put("items",records);
        return ResponseUtil.ok(data);

    }

    /**
     *   商品一级类目列表
     * @return
     */
    @GetMapping("/l1")
    public Object li(){
        List<CatVo> catVos = categroyService.queryCategory();
        return ResponseUtil.ok(catVos);
    }

    /**
     * 商品类目添加
     * @param category
     * @return
     */
    @PostMapping("/create")
    public Object add(@RequestBody DtsCategory category){
        DtsCategory dtsCategory = categroyService.add(category);
        return ResponseUtil.ok(dtsCategory);

    }

    /**
     * 商品类目编辑
     * @param category
     * @return
     */
    @PostMapping("/update")
    public Object updateCategory(@RequestBody DtsCategory category){
        int count = categroyService.updateCategroy(category);
        if (count==0){
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    /**
     * 商品类目删除
     * @param category
     * @return
     */
    @PostMapping("/delete")
    public Object deleteCateGory(@RequestBody DtsCategory category){
        categroyService.deleteCateGroy(category);
        return ResponseUtil.ok();
    }
}
